<template>
    <el-form label-width="100px" :rules="rules" ref="formRef" :model="form">
        <el-form-item label="公告名称" prop="announcementName">
            <el-input v-model="form.announcementName" aria-placeholder="请输入公告名称"></el-input>
        </el-form-item>
        <el-form-item label="公告内容" prop="announcementContent">
            <el-input v-model="form.announcementContent" type="textarea" :rows="4" aria-placeholder="请输入公告内容"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
        </el-form-item>
    </el-form>
</template>
<script setup>
import { ref, defineProps } from 'vue';
import { ElMessage } from 'element-plus';
import axios from 'axios';

const props = defineProps({
    form: {
        type: Object,
        default: () => {
            return {};
        }
    }
});
const form = ref({
    announcementName: '',
    announcementContent: ''
});
form.value = { ...props.form };

const rules = {
    announcementName: [
        { required: true, message: '请输入公告名称', trigger: 'blur' }
    ],
    announcementContent: [
        { required: true, message: '请输入公告内容', trigger: 'blur' }
    ]
};

const formRef = ref(null);


const submitForm = () => {
    formRef.value.validate(async (valid) => {
        if (valid) {
            await axios.post("api/announcement/update", form.value, {
                header: {
                    'Content-Type': 'application/json'
                }
            })
    ElMessage.success('提交成功!');
    console.log('表单数据：', form.value);
} else {
    ElMessage.error('请检查输入内容');
        }
    });
};


const resetForm = () => {
    formRef.value.resetFields();
};

</script>
<style lang="less" scoped>
.el-form {
    max-width: 500px;
    margin: 0 auto;
}
</style>
